/*
 *
 *  Copyright (C) 2010 Tal Eisenberg
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package fork.modbox;

import fork.Modbox;
import java.awt.Color;
import java.awt.Font;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JPanel;
import tal.drivechain.chain.Chain;
import tal.drivechain.gui.TextButton;
import tal.drivechain.module.properties.ColorParser;

/**
 *
 * @author eisental
 */
public class ChainPathPanel extends JPanel implements ActionListener {
    private Chain chain;
    private Color color, xor;
    private Modbox owner;
    private TextButton startStopButton;
    private TextButton scriptButton, embedButton, varButton;

    public ChainPathPanel(Modbox owner) {
        this.owner = owner;
        this.setBackground(Color.BLACK);
        this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
        setColor((Color)owner.getColor());
        setFont((Font)owner.getFont());
        setChain((Chain)owner.getActualBoxChain());
    }

    public void setChain(Chain chain) {
        List<TextButton> chainButtons = new ArrayList<TextButton>();
        this.chain = chain;
        this.removeAll();
        Chain curChain = chain;
        Color curColor = color;
        if (chain==null) return;

        do {
            String label;
            if (chainButtons.isEmpty())
                label = curChain.getName();
            else label = curChain.getName() + " >";
            Font f;
            if (curChain == chain) f = getFont().deriveFont((float)(getFont().getSize()*1.2));
            else f = getFont();
            TextButton b = new TextButton(label, curColor, f);
            b.setActionCommand(curChain.getPath());
            b.setHasBorder(false);
            b.setNegative(true);
            b.addActionListener(this);
            chainButtons.add(b);
            curChain = curChain.getParentChain();
            curColor = curColor.darker();
        } while (curChain!=null);

        for (int i=chainButtons.size()-1; i>=0; i--) {
            this.add(chainButtons.get(i));
        }
        this.add(Box.createHorizontalGlue());

        varButton = new TextButton(" variables... ", ColorParser.lighter(xor), getFont().deriveFont((float)(getFont().getSize()*1.1)));
        varButton.setNegative(false);
        varButton.setHasBorder(false);
        varButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                ChainPathPanel.this.owner.getGUI().showVariablesPopup(
                        new Point(varButton.getLocationOnScreen().x, varButton.getLocationOnScreen().y+varButton.getHeight()));
            }
        });
        this.add(varButton);

        if (chain.getScriptFilePath()!=null) {
            embedButton = new TextButton((chain.isLoadedFromFile?" embed ":" unembed "), ColorParser.lighter(xor), getFont().deriveFont((float)(getFont().getSize()*1.1)));
            embedButton.setToggleButton(true);
            embedButton.setSelected(!chain.isLoadedFromFile);
            embedButton.setHasBorder(false);
            embedButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    if (embedButton.isSelected()) {
                        ChainPathPanel.this.chain.isLoadedFromFile = false;
                        embedButton.setText(" unembed ");
                    } else {
                        ChainPathPanel.this.chain.isLoadedFromFile = true;
                        embedButton.setText(" embed ");
                    }
                }
            });
            embedButton.setNegative(false);
            this.add(embedButton);
        }

        scriptButton = new TextButton(" show script... ", ColorParser.lighter(xor), getFont().deriveFont((float)(getFont().getSize()*1.1)));
        scriptButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                owner.sendShowScript();
            }
        });
        scriptButton.setNegative(false);
        scriptButton.setHasBorder(false);
        this.add(scriptButton);

        // close/open chain button
        startStopButton = new TextButton(" stop ", ColorParser.lighter(xor), getFont().deriveFont((float)(getFont().getSize()*1.1)));
        startStopButton.setToggleButton(true);
        startStopButton.setSelected(chain.isRunning());
        startStopButton.setUnselectedText(" start ");
        startStopButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                if (startStopButton.isSelected()) ChainPathPanel.this.chain.start();
                else ChainPathPanel.this.chain.stop();
            }
        });
        startStopButton.setNegative(false);
        startStopButton.setHasBorder(false);
        this.add(startStopButton);
        this.validateTree();
        this.repaint();
    }

    public void setChainState(boolean open) {
        startStopButton.setSelected(open);
    }

    public void setColor(Color color) {
        this.color = color;
        this.xor = ColorParser.xor(color);
        if (chain!=null)
            this.setChain(chain);        
    }

    public void actionPerformed(ActionEvent e) {
        String sChain = ((TextButton)e.getSource()).getActionCommand();
        owner.setBoxChain(sChain, null);
    }
}

